package yt.kpool.activity.home;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.R;
import yt.kpool.adapter.PowAdapter;
import yt.kpool.base.BaseApplication;
import yt.kpool.bean.PowBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.SPUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.LoadingDialog;
import yt.kpool.view.pop.PowPop;

/**
 * Pow排行榜
 */
public class PowAc extends AppCompatActivity {
    @BindView(R.id.main_recycler)
    RecyclerView mRecyclerView;
    @BindView(R.id.edit)
    EditText edit;
    @BindView(R.id.ly_liner)
    LinearLayout ly_liner;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.image_button)
    ImageView image_button;
    private LoadingDialog dialog;
    private PowAdapter powAdapter;
    private List<PowBean.DataBean.CoinListBean> listData;
    private List<PowBean.DataBean.CoinListBean> listDatas = new ArrayList<>();
    private SPUtil spUtil;
    private PowPop powPop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pow);
        ButterKnife.bind(this);
        initTitle();
        init();
        if(NetWorkUtil.checkNet(PowAc.this))
        {
            getNetData();
        }
        else
        {

            ToastUtil.showToast(PowAc.this,getResources().getString(R.string.internet_error));
        }

    }

    private void init() {
        if (spUtil == null)
            spUtil = new SPUtil(this, "fileName");
        dialog = new LoadingDialog(this, 0);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        powAdapter = new PowAdapter(this, listData);
        mRecyclerView.setAdapter(powAdapter);

        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                listDatas.clear();
                for (int i = 0; i < listData.size(); i++) {
                    if (listData.get(i).getCode().toUpperCase().contains(s.toString().toUpperCase())) {
                        listDatas.add(listData.get(i));
                    }
                }
                powAdapter.setNewData(listDatas);
                powAdapter.notifyDataSetChanged();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        powPop = new PowPop(this, new PowPop.OnSelectItemListener() {
            @Override
            public void select(int i, String name) {
                tv_name.setText(name);
                if(i==0)
                {
                    sort(2);
                }
                else if(i==1)
                {
                    sort(1);
                }

                else if(i==2)
                {
                    sort(4);
                }
                else if(i==3)
                {
                    sort(3);
                }
                powAdapter.notifyDataSetChanged();
            }
        });

        final View getHeightView = new View(this);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                getHeightView.scrollBy(0, dy);
               if(getHeightView.getScrollY()>1000)
               {
                   image_button.setVisibility(View.VISIBLE);
               }
               else
               {
                   image_button.setVisibility(View.GONE);
               }
            }
        });

    }

    /**
     * 排序
     */
    private void sort(final int kind)
    {

        Collections.sort(listData, new Comparator<PowBean.DataBean.CoinListBean>() {

            @Override
            public int compare(PowBean.DataBean.CoinListBean o1, PowBean.DataBean.CoinListBean o2) {

                if(kind==1) // 24小时升序
                {
                    if (o1.getOutput24h() > o2.getOutput24h()) {
                        return 1;
                    }
                    if (o1.getOutput24h() == o2.getOutput24h()) {
                        return 0;
                    }
                    return -1;
                }
                else if(kind==2) //24小时倒序
                {
                    if (o1.getOutput24h() > o2.getOutput24h()) {
                        return -1;
                    }
                    if (o1.getOutput24h() == o2.getOutput24h()) {
                        return 0;
                    }
                    return 1;
                }
                else if (kind == 3) // 流通升序
                {
                    if (o1.getMarketCap() > o2.getMarketCap()) {
                        return 1;
                    }
                    if (o1.getMarketCap() == o2.getMarketCap()) {
                        return 0;
                    }
                    return -1;
                } else if (kind == 4)//流通倒序
                {
                    if (o1.getMarketCap() > o2.getMarketCap()) {
                        return -1;
                    }
                    if (o1.getMarketCap() == o2.getMarketCap()) {
                        return 0;
                    }
                    return 1;
                }

                return 0;

            }
        });
    }


    /**
     * 点击事件的处理
     *
     * @param view
     */
    @OnClick({R.id.ly_liner,R.id.image_button})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ly_liner:
                powPop.showAsDropDown(ly_liner, 0, 0);
                break;
            case R.id.image_button:
                mRecyclerView.smoothScrollToPosition(0);
//                image_button.setVisibility(View.GONE);
                break;
        }
    }
    /**
     * 设置标题栏
     */
    private void initTitle() {
        TitleControl t = new TitleControl(this);
        t.setTitle("PoW排行");

    }

    /**
     * 获取首页网络数据
     */
    private void getNetData()
    {
        dialog.show();
        NetHelperNew.PowCoinList(new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                PowBean powBean = MyGson.getInstance().fromJson(data, PowBean.class);
                if(powBean.getType()!=1)
                {
                    Toast.makeText(PowAc.this,powBean.getContent(),Toast.LENGTH_SHORT).show();
                }
                else
                {
                    listData=powBean.getData().getCoinList();
                    powAdapter.setNewData(listData);
                    powAdapter.setExchange(powBean.getData().getExchange());
                    if (spUtil.getInt(BaseApplication.MONEYKIND, 0) == 1)
                    {
                        powAdapter.setKind(1,0.0);


                    }
                    else if(spUtil.getInt(BaseApplication.MONEYKIND, 0) == 2)
                    {
                        powAdapter.setKind(2,0.0);


                    }
                    sort(2);
                    powAdapter.notifyDataSetChanged();
                }

                dialog.dismiss();

            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                dialog.dismiss();
                ToastUtil.showToast(PowAc.this,msg);
            }
        });

    }
}
